﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace iReaper.IndexService.Common.WebTools
{
    public class CookieAwareWebClient : WebClient
    {
        // Methods
        protected override WebRequest GetWebRequest(Uri address)
        {
            WebRequest request = base.GetWebRequest(address);
            HttpWebRequest httpRequest = request as HttpWebRequest;
            if (httpRequest == null)
            {
                return request;
            }
            httpRequest.CookieContainer = this.Cookies;
            httpRequest.AllowAutoRedirect = true;
            return httpRequest;
        }

        protected override WebResponse GetWebResponse(WebRequest request)
        {
            WebResponse response = base.GetWebResponse(request);
            HttpWebResponse httpResponse = response as HttpWebResponse;
            if (httpResponse == null)
            {
                return response;
            }
            this.Cookies.Add(httpResponse.Cookies);
            return httpResponse;
        }

        // Properties
        public virtual CookieContainer Cookies
        {
            get;
            set;
        }
    }


}
